/**
 * Project: apollo-base-tools
 * 
 * File Created at 2017年7月24日
 * 
 * Copyright 2015-2016 dx.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * DongXue software Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dx.com.
 */
package org.apollo.swing.commons;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;

/**
 * @ClassName: ConfirmableDialog
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author wuzhenfang(wzfbj2008@163.com)
 * @date 2017年7月24日 上午10:52:45
 * @version V1.0
 */
public class ConfirmableDialog extends JDialog {

	private static final long serialVersionUID = -461035402557238862L;

	public static final int OK = 0;
	public static final int CANCEL = 1;
	private JPanel contentPanel = null;
	private JPanel southPanel = null;
	protected Action okAction = new DynamicAction("确定", this, "doOK", new Object[0]);
	protected Action cancelAction = new DynamicAction("取消", this, "doCancel", new Object[0]);
	private int exitCode = 1;

	public ConfirmableDialog() {
		initialize();
	}

	public ConfirmableDialog(Frame owner) {
		super(owner);
		initialize();
	}

	public ConfirmableDialog(Dialog owner) {
		super(owner);
		initialize();
	}

	public ConfirmableDialog(Window owner) {
		super(owner);
		initialize();
	}

	public ConfirmableDialog(Frame owner, boolean modal) {
		super(owner, modal);
		initialize();
	}

	public ConfirmableDialog(Frame owner, String title) {
		super(owner, title);
		initialize();
	}

	public ConfirmableDialog(Dialog owner, boolean modal) {
		super(owner, modal);
		initialize();
	}

	public ConfirmableDialog(Dialog owner, String title) {
		super(owner, title);
		initialize();
	}

	public ConfirmableDialog(Window owner, Dialog.ModalityType modalityType) {
		super(owner, modalityType);
		initialize();
	}

	public ConfirmableDialog(Window owner, String title) {
		super(owner, title);
		initialize();
	}

	public ConfirmableDialog(Frame owner, String title, boolean modal) {
		super(owner, title, modal);
		initialize();
	}

	public ConfirmableDialog(Dialog owner, String title, boolean modal) {
		super(owner, title, modal);
		initialize();
	}

	public ConfirmableDialog(Window owner, String title, Dialog.ModalityType modalityType) {
		super(owner, title, modalityType);
		initialize();
	}

	public ConfirmableDialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc) {
		super(owner, title, modal, gc);
		initialize();
	}

	public ConfirmableDialog(Dialog owner, String title, boolean modal, GraphicsConfiguration gc) {
		super(owner, title, modal, gc);
		initialize();
	}

	public ConfirmableDialog(Window owner, String title, Dialog.ModalityType modalityType, GraphicsConfiguration gc) {
		super(owner, title, modalityType, gc);
		initialize();
	}

	private void initialize() {
		setContentPane(getContentPanel());
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				ConfirmableDialog.this.doCancel();
			}
		});
	}

	private JPanel getContentPanel() {
		if (this.contentPanel == null) {
			this.contentPanel = new JPanel();
			this.contentPanel.setLayout(new BorderLayout(5, 5));
			this.contentPanel.add(getSouthPanel(), "South");
			this.contentPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		}
		return this.contentPanel;
	}

	private JPanel getSouthPanel() {
		if (this.southPanel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(30);
			this.southPanel = new JPanel();
			this.southPanel.setLayout(flowLayout);
			JButton okBtn = new JButton(this.okAction);
			okBtn.setDefaultCapable(true);
			getRootPane().setDefaultButton(okBtn);
			this.southPanel.add(okBtn, null);
			this.southPanel.add(new JButton(this.cancelAction), null);
		}
		return this.southPanel;
	}

	public void setCenterComponent(JComponent centerComp) {
		getContentPanel().add(centerComp, "Center");
	}

	public final void doOK() {
		if (save()) {
			this.exitCode = 0;
			dispose();
		}
	}

	protected boolean save() {
		return true;
	}

	public final void doCancel() {
		this.exitCode = 1;
		dispose();
	}

	public int showDialog() {
		setVisible(true);
		return this.exitCode;
	}

	public Action getOkAction() {
		return this.okAction;
	}

	public Action getCancelAction() {
		return this.cancelAction;
	}
}
